package com.jdy.nettle.controller;

import com.jdy.entity.FrameUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.ArrayList;
import java.util.List;


/**
 * Thymeleaf的使用
 * <p>
 * 这里使用 Controller 注解， 可以直接定位到具体的页面，而不会将其解析成json
 */
@Controller
public class ThymeleafController {
    private static final Logger log = LoggerFactory.getLogger(ThymeleafController.class);

    @GetMapping("/index")
    public String index() {
        log.info("欢迎使用Thymeleaf！");
        return "index";
    }

    @GetMapping("/show")
    public String show(Model model) {
        /*单个数值*/
        model.addAttribute("str", "Hell! This is Thymeleaf!");

        /*对象*/
        FrameUser user = new FrameUser();
        user.setRowIndex(3);
        user.setUserName("张三");
        model.addAttribute("stu", user);

        List<FrameUser> list = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            FrameUser userTemp = new FrameUser();
            userTemp.setRowIndex(i + 1);
            userTemp.setUserName("张三" + i);
            list.add(userTemp);
        }

        model.addAttribute("users", list);

        return "index";
    }
}
